import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/31 10:02
 * @description 优化思路：利用前面已经形成的cur作为链表来绘制下一个cur
 */
public class Solution2 {
    public static void main(String[] args) {

    }
    public Node connect(Node root) {
        if (root == null) {
            return root;
        }
        // 第一行，只有根节点
        Node cur = root;
        while (cur != null) {
            Node dummy = new Node(0);
            Node pre = dummy;
            // 将这一行的所有节点都串成链表
            while (cur != null) {
                if (cur.left != null) {
                    pre.next = cur.left;
                    pre = pre.next;
                }
                if (cur.right != null) {
                    pre.next = cur.right;
                    pre = pre.next;
                }
                cur = cur.next;
            }
            cur = dummy.next;
        }
        return root;
    }
}
